package com.aivin.lib_readnotes.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.aivin.lib_base.tool.WkBrowserTool;
import com.aivin.lib_base.tool.WkLog;
import com.aivin.lib_readnotes.R;
import com.aivin.lib_readnotes.SelectCallBack;
import com.bumptech.glide.Glide;
import com.wk.bmoblib.bean.BookPureText;

import java.util.List;
import java.util.Locale;



public class BookShelfAdapter extends RecyclerView.Adapter {

    private List<BookPureText> book;
    private SelectCallBack selectCallBack;
    private Context mContext;

    public BookShelfAdapter(Context context, List<BookPureText> book, SelectCallBack selectCallBack) {
        this.book = book;
        this.mContext = context;
        this.selectCallBack = selectCallBack;
        this.setHasStableIds(true); //
    }

    @NonNull
    @Override
    public ChooseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_bookshelf, parent, false);
        return new ChooseHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        BookPureText bean = book.get(position);
        final ChooseHolder chooseHolder = (ChooseHolder) holder;
        chooseHolder.bookNameTv.setText(String.format(Locale.getDefault(), "%d、%s", bean.getBookIndex(), bean.getBookName()));

        Glide.with(mContext)
                .load(bean.getCoverImg())
                .placeholder(R.mipmap.ico_defalut_book2)
                .into(chooseHolder.bookItem);
        /*WkLog.showLog("---"+bean.getCoverImg());
        WkLog.showLog("---"+bean.getUrl());*/
    }


    @Override
    public int getItemCount() {
        return book == null ? 0 : book.size();
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    private class ChooseHolder extends RecyclerView.ViewHolder {

        private ImageView bookItem;
        private TextView bookNameTv;

        private ChooseHolder(View itemView) {
            super(itemView);
            bookItem = itemView.findViewById(R.id.bookItem);
            bookNameTv = itemView.findViewById(R.id.bookNameTv);
            View.OnClickListener clickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int mSelectedItem = getAdapterPosition();
                    selectCallBack.onSelected(book.get(mSelectedItem), mSelectedItem);
                    notifyDataSetChanged();
                }
            };
            bookItem.setOnClickListener(clickListener);

            bookItem.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int mSelectedItem = getAdapterPosition();
                    String url= book.get(mSelectedItem).getUrl();
                    WkBrowserTool.openDefaultBrowser(url ,mContext);
                    return false;
                }
            });

        }
    }


}
